package com.tongtech.system.controller;

import com.tongtech.common.core.web.controller.BaseController;
import com.tongtech.common.core.web.domain.AjaxResult;
import com.tongtech.common.log.annotation.Log;
import com.tongtech.common.log.enums.BusinessType;
import com.tongtech.common.security.annotation.RequiresPermissions;
import com.tongtech.common.security.utils.SecurityUtils;
import com.tongtech.system.domain.InformationCategory;
import com.tongtech.system.service.IInformationCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资讯分类信息
 *
 * @author xingyc
 */
@RestController
@RequestMapping("/infoCategory")
public class InformationCategoryController extends BaseController
{
    @Autowired
    private IInformationCategoryService infoCategoryService;

    /**
     * 获取资讯分类列表
     */
    @RequiresPermissions("info:category:list")
    @GetMapping("/list")
    public AjaxResult list(InformationCategory infoCategory)
    {
        List<InformationCategory> infoCategorys = infoCategoryService.selectInformationCategoryList(infoCategory);
        return success(infoCategorys);
    }

    /**
     * 根据资讯分类编号获取详细信息
     */
    @RequiresPermissions("info:category:query")
    @GetMapping(value = "/{infoCategoryId}")
    public AjaxResult getInfo(@PathVariable Long infoCategoryId)
    {
        return success(infoCategoryService.selectInformationCategoryById(infoCategoryId));
    }

    /**
     * 新增资讯分类
     */
    @RequiresPermissions("info:category:add")
    @Log(title = "资讯分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody InformationCategory infoCategory)
    {
        if (!infoCategoryService.checkInformationCategoryNameUnique(infoCategory))
        {
            return error("新增资讯分类'" + infoCategory.getCategoryName() + "'失败，资讯分类名称已存在");
        }
        infoCategory.setCreateBy(SecurityUtils.getUsername());
        return toAjax(infoCategoryService.insertInformationCategory(infoCategory));
    }

    /**
     * 修改资讯分类
     */
    @RequiresPermissions("info:category:edit")
    @Log(title = "资讯分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody InformationCategory dept)
    {
        dept.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(infoCategoryService.updateInformationCategory(dept));
    }

    /**
     * 删除资讯分类
     */
    @RequiresPermissions("info:category:remove")
    @Log(title = "资讯分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptId}")
    public AjaxResult remove(@PathVariable Long deptId)
    {
        if (infoCategoryService.hasChildByInformationCategoryId(deptId))
        {
            return warn("存在下级资讯分类,不允许删除");
        }
        return toAjax(infoCategoryService.deleteInformationCategoryById(deptId));
    }

    /**
     * 获取资讯分类列表
     */
    @RequiresPermissions("info:category:list")
    @GetMapping("/infoCategoryTree")
    public AjaxResult industryTree(InformationCategory infoCategory)
    {
        return success(infoCategoryService.selectInformationCategoryTreeList(infoCategory));
    }
}
